//
//  CCMemoryCache.m
//  Party
//
//  Created by coco on 15/3/15.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCMemoryCache.h"

@implementation CCMemoryCache

+ (instancetype)signalCache {
  static dispatch_once_t onceToken;
  static CCMemoryCache *cache;
  dispatch_once(&onceToken, ^{
    cache = [[CCMemoryCache alloc] init];
  });
  return cache;
}

- (id) init {
  if (self = [super init]) {
    _cacheObjs = [NSMutableDictionary dictionary];
  }
  return self;
}

- (BOOL) hasObjectForKey:(id)key {
  return  [_cacheObjs objectForKey:key]? YES : NO;
}

- (id)objectForKey:(id)key {
  return  [_cacheObjs objectForKey: key];
}

- (void)setObject:(id)object forKey:(id)key {
  if (key == nil) return;
  if (object == nil) return;
  id obj = [_cacheObjs objectForKey:key];
  if (obj == object) return;
  
  [_cacheObjs setObject:object forKey:key];
}
- (void)removeObjectForKey:(id)key {
  [_cacheObjs removeObjectForKey: key];
}
- (void)removeAllObjects {
  [_cacheObjs removeAllObjects];
}

- (id)objectForKeyedSubscript:(id)key {
  return [self objectForKey: key];
}

- (void)setObject:(id)obj forKeyedSubscript:(id)key {
  [self setObject:obj forKey:key];
}

@end
